package com.jxc.action.hygl;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.jxc.action.BaseAction;
import com.jxc.model.vo.Json;
import com.jxc.model.vo.SessionInfo;
import com.jxc.model.vo.VMemberType;
import com.jxc.model.vo.VRepertory;
import com.jxc.model.vo.VUser;
import com.jxc.service.MemberTypeServiceI;
import com.jxc.service.RepertoryServiceI;
import com.jxc.service.UserServiceI;
import com.jxc.util.ExceptionUtil;
import com.jxc.util.ResourceUtil;
import com.opensymphony.xwork2.ModelDriven;

@Namespace("/hygl")
@Action(value = "memberTypeAction", results = {
		@Result(name = "memberType", location = "/member/memberType.jsp"),
		@Result(name = "memberTypeAdd", location = "/member/memberTypeAdd.jsp"),
		@Result(name = "memberTypeEdit", location = "/member/memberTypeEdit.jsp") })
public class MemberTypeAction extends BaseAction implements
		ModelDriven<VMemberType> {
	private final static Logger logger = Logger
			.getLogger(MemberTypeAction.class);
	@Autowired
	private MemberTypeServiceI memberTypeService;
	@Autowired
	private UserServiceI userService;
	@Autowired
	private RepertoryServiceI repertoryService;

	private VMemberType model = new VMemberType();

	@Override
	public VMemberType getModel() {
		return model;
	}

	public String memberType() {
		getSubAuths("HYGL_HYLX");
		return "memberType";
	}

	public String memberTypeAdd() {
		return "memberTypeAdd";
	}

	public String memberTypeEdit() {
		return "memberTypeEdit";
	}

	public void add() {
		Json j = new Json();
		try {
			SessionInfo sessionInfo = (SessionInfo) ServletActionContext
					.getRequest().getSession().getAttribute(
							ResourceUtil.getSessionInfoName());
			VUser vuser = userService.get(sessionInfo.getUserId());
			if (vuser.getReid() == null) {
				model.setUid(vuser.getId());
			} else {
				VRepertory vRepertory = repertoryService.get(vuser.getReid());
				model.setUid(vRepertory.getUid());
			}
			memberTypeService.save(model);
			j.setMsg("添加成功！");
			j.setSuccess(true);
		} catch (Exception e) {
			j.setMsg("添加失败！");
			logger.info(ExceptionUtil.getExceptionMessage(e));
		}
		super.writeJson(j);
	}

	public void edit() {
		Json j = new Json();
		try {
			SessionInfo sessionInfo = (SessionInfo) ServletActionContext
					.getRequest().getSession().getAttribute(
							ResourceUtil.getSessionInfoName());
			VUser vuser = userService.get(sessionInfo.getUserId());
			if (vuser.getReid() == null) {
				model.setUid(vuser.getId());
			} else {
				VRepertory vRepertory = repertoryService.get(vuser.getReid());
				model.setUid(vRepertory.getUid());
			}
			memberTypeService.update(model);
			j.setSuccess(true);
			j.setMsg("编辑成功！");
		} catch (Exception e) {
			logger.error(ExceptionUtil.getExceptionMessage(e));
			j.setMsg("编辑失败！");
		}
		super.writeJson(j);
	}

	public void delete() {
		Json j = new Json();
		memberTypeService.delete(model.getIds());
		j.setSuccess(true);
		j.setMsg("删除成功!");
		super.writeJson(j);
	}

	public void datagrid() {
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext
				.getRequest().getSession().getAttribute(
						ResourceUtil.getSessionInfoName());
		VUser vuser = userService.get(sessionInfo.getUserId());
		if (vuser.getReid() == null) {
			model.setUid(vuser.getId());
		} else {
			VRepertory vRepertory = repertoryService.get(vuser.getReid());
			model.setUid(vRepertory.getUid());
		}
		super.writeJson(memberTypeService.datagrid(model));
	}

	public void noAuth_combobox() {
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext
				.getRequest().getSession().getAttribute(
						ResourceUtil.getSessionInfoName());
		VUser vuser = userService.get(sessionInfo.getUserId());
		if (vuser.getReid() == null) {
			model.setUid(vuser.getId());
		} else {
			VRepertory vRepertory = repertoryService.get(vuser.getReid());
			model.setUid(vRepertory.getUid());
		}
		super.writeJson(memberTypeService.combobox(model));
	}

}
